package com.mingqi.enetconnected.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mingqi.enetconnected.R;
import com.mingqi.enetconnected.util.resource.ResourceUtils;

public class HorizontalLayout extends LinearLayout {

    private TextView leftTextView;
    private TextView rightTextView;
    private LinearLayout layout;
    private ImageView imageView;

    public HorizontalLayout(Context context) {
        this(context, null, 0);
    }

    public HorizontalLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HorizontalLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.view_horizontal_layout, this);

        leftTextView = findViewById(R.id.left_text);
        rightTextView = findViewById(R.id.right_text);
        layout = findViewById(R.id.text_layout);
        imageView = findViewById(R.id.right_icon);
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.HorizontalLayout, defStyleAttr, 0);
        int indexCount = typedArray.getIndexCount();
        for (int i = 0; i < indexCount; i++) {
            int index = typedArray.getIndex(i);
            if (index == R.styleable.HorizontalLayout_leftText) {
                setLeftText(typedArray.getString(index));
            } else if (index == R.styleable.HorizontalLayout_leftTextColor) {
                setLeftTextViewColor(typedArray.getColor(index, ResourceUtils.getColor(R.color.color_black_333333)));
            } else if (index == R.styleable.HorizontalLayout_rightText) {
                setRightText(typedArray.getString(index));
            } else if (index == R.styleable.HorizontalLayout_rightTextColor) {
                setRightTextViewColor(typedArray.getColor(index, ResourceUtils.getColor(R.color.color_grey_999999)));
            } else if (index == R.styleable.HorizontalLayout_imageVisibility) {
                setImageVisibility(typedArray.getBoolean(index, true));
            }
        }
        typedArray.recycle();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }


    public void setOnClickListener(View.OnClickListener listener) {
        layout.setOnClickListener(listener);
    }

    public void setLeftTextViewColor(int color) {
        leftTextView.setTextColor(color);
    }

    public void setRightTextViewColor(int color) {
        rightTextView.setTextColor(color);
    }

    public void setLeftText(int text) {
        leftTextView.setText(text);
    }

    public void setLeftText(String text) {
        leftTextView.setText(text);
    }

    public void setRightText(int text) {
        rightTextView.setText(text);
    }

    public void setRightText(String text) {
        rightTextView.setText(text);
    }

    public void setImageVisibility(boolean flag) {
        imageView.setVisibility(flag ? View.VISIBLE : View.INVISIBLE);
    }
}
